// 获取连接参数值
export const getUrlParams = (name) => {
  const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  const r = window.location.search.substr(1).match(reg)
  if (r != null) {
    return unescape(r[2])
  }
  return null
}
// 判断是否在app中使用页面
export const isInnerApp = () => {
  return /uotta/i.test(navigator.userAgent)
}
// 判断设备类型
export const getDeviceType = () => {
  const u = navigator.userAgent
  const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 // android终端
  const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
  if (isAndroid) {
    return 'android'
  } else if (isiOS) {
    return 'ios'
  } else {
    return 'pc'
  }
}
// 获取请求id
export const getRequestId = () => {
  let random = Math.random()
  let number = ''
  if (random > 0.1) {
    number = Math.round(random * 1000000)
  } else {
    random += 0.1
    number = Math.round(random * 1000000)
  }
  return number.toString() + new Date().valueOf().toString()
}

// 节流 节流函数的作用是规定一个单位时间，在这个单位时间内最多只能触发一次函数执行，如果这个单位时间内多次触发函数，只能有一次生效.
export function throttle(func, delay) {
  var timer = null;
  var startTime = Date.now();
  return function () {
    var curTime = Date.now();
    var remaining = delay - (curTime - startTime);
    var context = this;
    var args = arguments;
    clearTimeout(timer);
    if (remaining <= 0) {
      func.apply(context, args);
      startTime = Date.now();
    } else {
      timer = setTimeout(func, remaining);
    }
  }
}
/**
 *
 if value is not null, return value and add unit;else return value;
 * @param  {string}value
 * @param  {string}unit
 *@returns {string}value+unit or value
 */
export function addsign(value = '', unit = '') {
  value = value || '-'
  return value + (['', '-'].includes(value) ? '' : unit)
}
/**
 * 判断数据为JSON格式
 * input 数据
 *return true or false
 */
export function isJson(obj) {
  var isjson = typeof (obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;
  return isjson;
}
/**
 * 将JSON数据逐一判空转为-
 * @param { object }obj
 * @returns { object}obj
 */
export function json2Null(obj) {
  if (isJson(obj)) {
    Object.keys(obj).forEach((keyChild) => {
      obj[keyChild] = obj[keyChild] || "-";
    });
    return obj;
  } else
    return obj;
}

export function formatDate(date) {
  if (!date) return ''
  let year = date.getFullYear();
  let month = (date.getMonth() + 1).toString().padStart(2, '0');  // 月要+1
  let day = date.getDate().toString().padStart(2, '0');  // 获取天是getDate，而不是 getDay
  let hour = date.getHours().toString().padStart(2, '0');
  let minute = date.getMinutes().toString().padStart(2, '0');
  return year + '-' + month + '-' + day + ' ' + hour + ':' + minute;;
}
// 深拷贝
export function deepClone(source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone')
  }
  const targetObj = source.constructor === Array ? [] : {}
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}

// 格式化显示间隔时间
export function formatSeconds (value) {
  let s = parseInt(value)// 秒
  let m = 0// 分
  let h = 0// 小时
  if (s > 60) {
    m = parseInt(s / 60)
    s = parseInt(s % 60)
    if (m > 60) {
      h = parseInt(m / 60)
      m = parseInt(m % 60)
    }
  }
  let result = '' + parseInt(s)
  if (result < 10) {
    result = '0' + result
  }
  if (m > 0) {
    result = '' + parseInt(m) + ':' + result
    if (m < 10) {
      result = '0' + result
    }
  } else {
    result = '00:' + result
  }
  if (h > 0) {
    result = '' + parseInt(h) + ':' + result
    if (h < 10) {
      result = '0' + result
    }
  } else {
    result = '00:' + result
  }
  return result
}
